import {createDOM} from '../react-dom';

class Component {
  static isReactComponent = true; //用来区分是函数还是类组件
  constructor(props) {
    this.props = props;
  }

  render(){
    throw new Error('此方法为抽象方法,需要子类实现');
  }

  setState(partialState){
    console.log('setState');
    const state = this.state;
    this.state = {...state, ...partialState};

    const newVdom = this.render();
    updateClassComponent(this,newVdom);
  }
}

function updateClassComponent(/*currentFiber*/classInstance, newVdom) {
  const oldDOM = classInstance.dom; //在 mountClassComponent 里挂的
  const newDOM = createDOM(newVdom)
  oldDOM.parentNode.replaceChild(newDOM, oldDOM);
  classInstance.dom = newDOM;
}

export default Component;
